# 设置编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -O3")
# 设置库文件的输出地址
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# 设定库源文件文件夹
aux_source_directory(geometry/ GCTL_GEOMETRY_SRC)
aux_source_directory(algorithm/ GCTL_ALGORITHM_SRC)
aux_source_directory(io/ GCTL_IO_SRC)
aux_source_directory(mesh/ GCTL_MESH_SRC)
aux_source_directory(utility/ GCTL_UTILITY_SRC)
aux_source_directory(algebra/ GCTL_ALGEBRA_SRC)
aux_source_directory(geodesy/ GCTL_GEODESY_SRC)
# 在windows系统下使用改编过的getopt_win源文件替代GNU getopt
# 若在linux系统下则不适用此源文件
if(NOT WIN32)
	list(REMOVE_ITEM GCTL_UTILITY_SRC "utility//getopt_win.c")
endif()

if(NOT GCTL_FFTW3)
	list(REMOVE_ITEM GCTL_ALGORITHM_SRC "algorithm//fft.cpp")
endif()

if(NOT GCTL_NETCDF)
	list(REMOVE_ITEM GCTL_IO_SRC "io//netcdf_io.cpp")
endif()

# 以下部分为库的编译
# 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库
# 注意此处不必为目标名称添加lib前缀和相应后缀，cmake会自行添加
add_library(gctl SHARED ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHM_SRC} ${GCTL_GEODESY_SRC}
	${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MESH_SRC} ${GCTL_ALGEBRA_SRC})
# 首先添加静态库的生成命令
add_library(gctl_static STATIC ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHM_SRC} ${GCTL_GEODESY_SRC}
	${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MESH_SRC} ${GCTL_ALGEBRA_SRC})
# 设置静态库的输出名称从而获得与动态库名称相同的静态库
set_target_properties(gctl_static PROPERTIES OUTPUT_NAME "gctl")
# 设置输出目标属性以同时输出动态库与静态库
set_target_properties(gctl PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(gctl_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# 设置动态库的版本号
set_target_properties(gctl PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
# 设置动态库的运行搜索地址
set_target_properties(gctl PROPERTIES INSTALL_RPATH /usr/local/lib)
set_target_properties(gctl_static PROPERTIES INSTALL_RPATH /usr/local/lib)

if(GCTL_WAVELIB)
	target_link_libraries(gctl ${WaveLib_LIB})
	target_link_libraries(gctl_static ${WaveLib_LIB})
endif()

if(GCTL_NETCDF)
	target_link_libraries(gctl ${netCDF_CXX_LEGACY_LIB})
	target_link_libraries(gctl_static ${netCDF_CXX_LEGACY_LIB})
	target_link_libraries(gctl ${netCDF_LIBRARIES})
	target_link_libraries(gctl_static ${netCDF_LIBRARIES})
endif()

# 连接动态库与静态库
if(GCTL_FFTW3)
	# 因为是手动设置的动态库地址 使用find_library更保险
	find_library(LIB_TAR ${FFTW3_LIB} HINTS ${FFTW3_LIB_DIR})
	target_link_libraries(gctl ${LIB_TAR})
	target_link_libraries(gctl_static ${LIB_TAR})
endif()

if(GCTL_EEMD)
	target_link_libraries(gctl ${EEMD_LIB})
	target_link_libraries(gctl_static ${EEMD_LIB})
endif()

if(GCTL_OPENMP)
	target_link_libraries(gctl OpenMP::OpenMP_CXX)
	target_link_libraries(gctl_static OpenMP::OpenMP_CXX)
endif()

if(GCTL_OPENBLAS)
	target_link_libraries(gctl ${OpenBLAS_LIBRARIES})
	target_link_libraries(gctl_static ${OpenBLAS_LIBRARIES})
endif()

set(CONFIG_FILE_PATH lib/cmake/${PROJECT_NAME})
configure_package_config_file(${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in 
	${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
	INSTALL_DESTINATION ${CONFIG_FILE_PATH})

write_basic_package_version_file(${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
	VERSION ${PROJECT_VERSION}
	COMPATIBILITY SameMajorVersion)

# 库的安装命令
if(WIN32)
	install(TARGETS gctl DESTINATION lib)
	install(TARGETS gctl_static DESTINATION lib)
else()
	install(TARGETS gctl gctl_static
		EXPORT ${PROJECT_NAME}Targets
		LIBRARY DESTINATION lib
		ARCHIVE DESTINATION lib)
	install(EXPORT ${PROJECT_NAME}Targets
		DESTINATION ${CONFIG_FILE_PATH})
	install(FILES
		${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
		${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
		DESTINATION ${CONFIG_FILE_PATH})
endif()

# 头文件安装命令
file(GLOB GCTL_HEAD *.h)
file(GLOB GCTL_CORE_HEAD core/*.h)
file(GLOB GCTL_IO_HEAD io/*.h)
file(GLOB GCTL_ALGORITHM_HEAD algorithm/*.h)
file(GLOB GCTL_ALGEBRA_HEAD algebra/*.h)
file(GLOB GCTL_GEOMETRY_HEAD geometry/*.h)
file(GLOB GCTL_UTILITY_HEAD utility/*.h)
file(GLOB GCTL_MESH_HEAD mesh/*.h)
file(GLOB GCTL_GEODESY_HEAD geodesy/*.h)

if(NOT WIN32)
	list(REMOVE_ITEM GCTL_UTILITY_HEAD "${PROJECT_SOURCE_DIR}/lib/utility/getopt_win.h")
endif()

if(NOT GCTL_FFTW3)
	list(REMOVE_ITEM GCTL_ALGORITHM_HEAD "${PROJECT_SOURCE_DIR}/lib/algorithm/fft.h")
endif()

if(NOT GCTL_NETCDF)
	list(REMOVE_ITEM GCTL_IO_HEAD "${PROJECT_SOURCE_DIR}/lib/io/netcdf_io.h")
endif()

install(FILES ${GCTL_HEAD} DESTINATION include/gctl)
install(FILES ${GCTL_CORE_HEAD} DESTINATION include/gctl/core)
install(FILES ${GCTL_IO_HEAD} DESTINATION include/gctl/io)
install(FILES ${GCTL_ALGEBRA_HEAD} DESTINATION include/gctl/algebra)
install(FILES ${GCTL_ALGORITHM_HEAD} DESTINATION include/gctl/algorithm)
install(FILES ${GCTL_GEOMETRY_HEAD} DESTINATION include/gctl/geometry)
install(FILES ${GCTL_UTILITY_HEAD} DESTINATION include/gctl/utility)
install(FILES ${GCTL_MESH_HEAD} DESTINATION include/gctl/mesh)
install(FILES ${GCTL_GEODESY_HEAD} DESTINATION include/gctl/geodesy)